import request from '@/plugin/axios'
import { sysPath } from '@api/sys.common'

export function LoadMenu(params) {
  let api = sysPath + "/hx-mart/api/v1sys/sysUser/getSysUserMenu";
  return request({
    url: api,
    method: 'get',
    params
  })
}
// 菜单 侧边栏
export let mAside = [
  // sysManager,
  // maExamples
]

// 菜单 顶栏
export let mHeader = [
  {
    path: '/index',
    title: '首页',
    icon: 'home'
  }
  // sysManager,
  // maExamples
]
// 菜单 顶栏
export function initMenu(data) {
  // console.info(JSON.stringify(data));
  mHeader.splice(0, mHeader.length)
  mAside.splice(0, mAside.length)
  let menu = {}
  data.forEach(element => {
    menu = delChildren(element)
    mHeader = mHeader.filter(function (item) {
      return JSON.stringify(item) !== JSON.stringify(menu)
    })
    mAside = mAside.filter(function (item) {
      return JSON.stringify(item) !== JSON.stringify(menu)
    })
    mHeader.push(menu)
    mAside.push(menu)
  })
}

function delChildren(data) {
  if (data.children.length > 0) {
    data.children.forEach(element => {
      delChildren(element)
    })
  } else {
    delete data.children
  }
  return data
}
